package service.serviceImpl;

import dao.CourseDao;
import dao.CourseStudentDao;
import dao.Impl.CourseDaoImpl;
import dao.Impl.CourseStudentDaoImpl;
import po.Course;
import po.CourseStudent;
import service.CourseService;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class CourseServiceImpl implements CourseService {

    private CourseDao dao=new CourseDaoImpl();
    private CourseStudentDao dao2=new CourseStudentDaoImpl();
    @Override
    public void delete(Course course) {
        dao.delete(course);
    }

    @Override
    public void insert(Course course) {
        dao.insert(course);
    }

    @Override
    public void update(Course course) {
           dao.update(course);
    }


    @Override
    public List<Course> queryAll(Integer collegeId,Integer gradeId) {
        List<Course> courses = dao.queryAllCourse(collegeId,gradeId);
        return courses;
    }




    @Override
    public List<Course> queryChooseCourse(Integer sid) {
        ArrayList<Course> objects = new ArrayList<>();
        List<CourseStudent> courseStudents = dao2.queryAll(sid);
        for (CourseStudent courseStudent : courseStudents) {
            Course course = dao.queryOne(courseStudent.getCid());
            objects.add(course);
        }
        return objects;
    }


    @Override
    public boolean ifReachBeginTime(Integer cid) {
        String beginTime = dao.getBeginTime(cid);
        Long longStr = new Long(beginTime.replaceAll("[^\\d]+", ""));
        SimpleDateFormat tempDate = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String datetime = tempDate.format(new java.util.Date());
        Long longStr2 = new Long(datetime.replaceAll("[^\\d]+", ""));
        int i = longStr.compareTo(longStr2);
        if(i>0){
            return false;
        }else {
            return  true;
        }


    }

    @Override
    public Course queryOne(Integer id) {
        Course course = dao.queryOne(id);
        return course;
    }
}
